package com.ryder.ryderschool.controller;

import com.ryder.ryderschool.common.Result;
import com.ryder.ryderschool.entity.ProjectFunding;
import com.ryder.ryderschool.service.ProjectFundingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "项目资金管理", description = "项目资金相关接口")
@RestController
@RequestMapping("/projects/{projectId}/fundings")
@RequiredArgsConstructor
public class ProjectFundingController {

    private final ProjectFundingService projectFundingService;

    @Operation(summary = "创建项目资金记录")
    @PostMapping
    public Result<Void> createFunding(
            @Parameter(description = "项目ID") @PathVariable Integer projectId,
            @RequestBody ProjectFunding funding) {
        funding.setProjectId(projectId);
        projectFundingService.createFunding(funding);
        return Result.success();
    }

    @Operation(summary = "更新项目资金记录")
    @PutMapping("/{id}")
    public Result<Void> updateFunding(
            @Parameter(description = "项目ID") @PathVariable Integer projectId,
            @Parameter(description = "资金记录ID") @PathVariable Integer id,
            @RequestBody ProjectFunding funding) {
        funding.setId(id);
        funding.setProjectId(projectId);
        projectFundingService.updateFunding(funding);
        return Result.success();
    }

    @Operation(summary = "删除项目资金记录")
    @DeleteMapping("/{id}")
    public Result<Void> deleteFunding(
            @Parameter(description = "项目ID") @PathVariable Integer projectId,
            @Parameter(description = "资金记录ID") @PathVariable Integer id) {
        projectFundingService.deleteFunding(id);
        return Result.success();
    }

    @Operation(summary = "获取项目资金记录")
    @GetMapping("/{id}")
    public Result<ProjectFunding> getFunding(
            @Parameter(description = "项目ID") @PathVariable Integer projectId,
            @Parameter(description = "资金记录ID") @PathVariable Integer id) {
        return Result.success(projectFundingService.getFunding(id));
    }

    @Operation(summary = "获取项目资金记录列表")
    @GetMapping
    public Result<?> listFundings(
            @Parameter(description = "项目ID") @PathVariable Integer projectId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(projectFundingService.listFundings(projectId, pageNum, pageSize));
    }

    @Operation(summary = "审批项目资金记录")
    @PostMapping("/{id}/approve")
    public Result<Void> approveFunding(
            @Parameter(description = "项目ID") @PathVariable Integer projectId,
            @Parameter(description = "资金记录ID") @PathVariable Integer id,
            @Parameter(description = "是否通过") @RequestParam Boolean approved,
            @Parameter(description = "审批意见") @RequestParam(required = false) String comment) {
        projectFundingService.approveFunding(id, approved, comment);
        return Result.success();
    }
} 